<?php

namespace App\Http\Controllers\WorkFlow;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\ProcessStep;
use Illuminate\Http\Request;

class ProcessStepController extends Controller
{
    use ResponseData;
    //
    public function save(Request $request, ProcessStep $model){
        if(is_array($request->data)){
            $data = $request->data;
        }else{
            $data = json_decode($request->data,true);
        }
        foreach ($data as $item){
            // 查询是否存在 如果存在则更新
            $this->setStepFlow($request,$item);
        }

        return $this->success('更新成功');
    }

    protected function setStepFlow($request,$step_order){
        $step = ProcessStep::query()
            ->where('flow_id',$request->flow_id)
            ->where('step_id',$step_order['step_order'])
            ->first();
        if($step) {
//            $step->step_name = $request->step_name;
//            $step->description = $request->description;
//            $step->required_time = $request->required_time;
            $step->prev_step_id = $request->prev_step_id;
            $step->next_step_id = $request->next_step_id;
            $step->save();
        }else{
            $step_order['flow_id'] = $request->flow_id;
            $step = ProcessStep::query()
                ->create($step_order);

        }
    }
}
